﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace ExportListDatatoCSV.Layouts.ExportListDatatoCSV
{
    public partial class ExporttoCSV : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropDown(); 
            }
            Page.RegisterClientScriptBlock("Validate", "<script type='text/javascript'>_spOriginalFormAction=document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;</script> "); 
              
        }

        private void BindDropDown()
        {
            ListItem ltData ;
            SPListCollection lstColl = SPContext.Current.Web.Lists;
            foreach (SPList lst in lstColl)
            {
                if (!lst.Hidden)
                {
                    ltData = new ListItem(lst.Title.ToString(), lst.ID.ToString());
                    ddlList.Items.Add(ltData);
                }
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Guid gd = new Guid(ddlList.SelectedValue);
                ExportToCSV(SPContext.Current.Web.Lists[gd].Items);
               
            }
            catch (Exception ex)
            {
                lbError.Visible = true;
                lbError.Text = ex.Message;
            }
        }

        private void ExportToCSV(SPListItemCollection  dtData)
        {
            Response.ContentType = "Application/x-msexcel";
            Response.AddHeader("content-disposition", "attachment;filename="+ddlList.SelectedItem.Text.Replace(" ","")  +".csv");
            Response.Write(GetCSV(dtData));
            Response.End();
        }

        private string GetCSV(SPListItemCollection listItemColl)
        {
            StringBuilder sb = new StringBuilder();
            if (listItemColl.Count != 0)
            {
                foreach (SPField column in listItemColl.Fields )
                {
                    if (!column.Hidden)
                    {
                        sb.Append(column.Title.Replace(",", "") + ',');
                    }
                }
                sb.Append("\r\n");

                foreach (SPListItem  row in listItemColl)
                {
                    foreach (SPField column in listItemColl.Fields)
                    {
                        if (!column.Hidden)
                        {
                            sb.Append(Convert.ToString(row[column.InternalName]).Replace(",", "") + ',');
                        }
                    }
                    sb.Append("\r\n");
                }
            }
            return sb.ToString();
        }
    }
}
